<?php

namespace App\Console\Commands\OpenShop;

use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class OrderManagerBuild extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:buildOrderManager';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '开店 : 构建历史订单涉及负责人数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        OpenShopMainProcessModel::query()->chunkById(500, function(Collection $process){
            $process->each(function(OpenShopMainProcessModel $openShopMainProcessModel){
                $this->getOutput()->info($openShopMainProcessModel->id);
                if(($signOrder = $openShopMainProcessModel->signOrderRel) && $signOrder->investmentManagerId > 0) {
                    OpenShopOrderManagerModel::generateManagerOrUpdate(
                        OpenShopOrderManagerModel::MANAGER_TYPE_1,
                        $signOrder->mainProcessId,
                        $signOrder->investmentManagerId,
                        $signOrder->investmentManagerName,
                        $signOrder->status
                    );

                    if(($shopLocation = $openShopMainProcessModel->shopLocationRel) && $shopLocation->investmentManagerId > 0){
                        OpenShopOrderManagerModel::generateManagerOrUpdate(
                            OpenShopOrderManagerModel::MANAGER_TYPE_2,
                            $signOrder->mainProcessId,
                            $shopLocation->investmentManagerId,
                            $shopLocation->investmentManagerName,
                            $signOrder->status
                        );

                        if($designer = $openShopMainProcessModel->spaceDesignRel){
                            if($designer->designUid > 0){
                                OpenShopOrderManagerModel::generateManagerOrUpdate(
                                    OpenShopOrderManagerModel::MANAGER_TYPE_3,
                                    $signOrder->mainProcessId,
                                    $designer->designUid,
                                    $designer->designName,
                                    $signOrder->status
                                );
                            }

                            if($designer->preparationUid > 0){
                                OpenShopOrderManagerModel::generateManagerOrUpdate(
                                    OpenShopOrderManagerModel::MANAGER_TYPE_4,
                                    $signOrder->mainProcessId,
                                    $designer->preparationUid,
                                    $designer->preparationName,
                                    $signOrder->status
                                );

                                if(($checkAccept = $openShopMainProcessModel->checkAcceptRel) && $checkAccept->managerId > 0){
                                    OpenShopOrderManagerModel::generateManagerOrUpdate(
                                        OpenShopOrderManagerModel::MANAGER_TYPE_5,
                                        $signOrder->mainProcessId,
                                        $checkAccept->managerId,
                                        $checkAccept->managerName,
                                        $signOrder->status
                                    );
                                }
                            }
                        }
                    }
                }
            });
        });

        return 0;
    }
}
